/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-4-18
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.tools;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.springyounger.lottery.dal.dataobject.LotteryPhase;
import com.springyounger.lottery.dal.dataobject.LotteryStatistical;
import com.springyounger.lottery.service.user.PhaseService;

/**
 * TODO Comment of AdminQuartz
 * 
 * @author springyounger
 */
public class AdminQuartz {
    private PhaseService phaseService;
    private Logger       logger = Logger.getLogger(getClass());

    public void insert() {
        //当前日期
        Date today = LotteryUtil.getToday();
        List<LotteryPhase> allPhaseList = phaseService.getPhases();
        List<LotteryPhase> phaseList = new ArrayList<LotteryPhase>();
        for (LotteryPhase phase : allPhaseList) {
            if (LotteryUtil.getQuot(today, phase.getGmtStart()) <= 0
                    && LotteryUtil.getQuot(today, phase.getGmtEnd()) >= 0) {
                phaseList.add(phase);
            }
        }

        List<LotteryStatistical> statList = new ArrayList<LotteryStatistical>();

        if (logger.isInfoEnabled()) {
            logger.info("spring quartz 统计活动人数开始");
        }

        boolean isExistStat = false;
        
        for (LotteryPhase phase : phaseList) {
            //今天已经更新了
            if (phaseService.isExistStat(phase.getPhaseId(), today)) {
                if (logger.isInfoEnabled()) {
                    logger.info("今天的活动人数已经更新");
                }
                isExistStat = true;
                break;
            }
            //各种等级
            for (int i = 1; i <= LotteryStatistical.LEVELNUM; i++) {
                LotteryStatistical nowstat = new LotteryStatistical();
                nowstat = phaseService.getStatValue(phase.getPhaseId(), String.valueOf(i), today);
                if (null == nowstat) {
                    LotteryStatistical nullstat = new LotteryStatistical();
                    nullstat.setPhaseId(phase.getPhaseId());
                    nullstat.setPrizeLevel(String.valueOf(i));
                    nullstat.setWinnerAmount(0);
                    nullstat.setWinDate(today);
                    statList.add(nullstat);
                } else {
                    nowstat.setWinDate(today);
                    statList.add(nowstat);
                }
            }
        }
        if (!isExistStat) {
            //批量创建
            if (phaseService.batchInsertStat(statList)) {
                if (logger.isInfoEnabled()) {
                    logger.info("批量统计活动人数成功");
                }
            } else {
                if (logger.isInfoEnabled()) {
                    logger.info("批量统计活动人数失败");
                }
            }
        }
        if (logger.isInfoEnabled()) {
            logger.info("spring quartz 统计活动人数结束");
        }
    }

    /**
     * @param phaseService the phaseService to set
     */
    public void setPhaseService(PhaseService phaseService) {
        this.phaseService = phaseService;
    }

}
